package com.hundsun.cprs.yyt.web.action.report;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hundsun.cprs.yyt.service.account.domain.AccountQuotaDaily;
import com.hundsun.cprs.yyt.service.account.domain.query.AccountQuotaDailyQuery;
import com.hundsun.cprs.yyt.service.account.service.AccountQuotaDailyService;
import com.hundsun.cprs.yyt.service.quota.service.QuotaService;
import com.hundsun.cprs.yyt.util.DateUtil;
import com.hundsun.cprs.yyt.util.excel.EasyExcelUtil;
import com.hundsun.cprs.yyt.web.action.BaseAction;
import com.hundsun.cprs.yyt.web.vo.excel.AccountQuotaDailyExcelVo;

@Controller
public class AccountQuotaDailyReport extends BaseAction{

    @Autowired
    private AccountQuotaDailyService accountQuotaDailyService;
    @Autowired
    private QuotaService quotaService;
    
    @RequestMapping("/report/account_quota_daily")
    public void report(@ModelAttribute("query") AccountQuotaDailyQuery query,ModelMap model){
        checkRequest(query);
        accountQuotaDailyService.selectByPage(query);
        
        model.put("quotaList", quotaService.queryAll());
        
        if (query.getTotalCount() > 0) {
            AccountQuotaDailyQuery sumQuery = new AccountQuotaDailyQuery();
            BeanUtils.copyProperties(query, sumQuery);
            AccountQuotaDaily sumResult = accountQuotaDailyService.sumByQuery(sumQuery);
            model.put("sumResult", sumResult);
        }
    }
    
    @RequestMapping("/report/account_quota_daily/download")
    public void reportDownload(@ModelAttribute("query") AccountQuotaDailyQuery query,HttpServletResponse response){
        checkRequest(query);
        
        query.setPageSize(40000);
        accountQuotaDailyService.selectByPage(query);
        
        
        List<AccountQuotaDailyExcelVo> exportData = new ArrayList<AccountQuotaDailyExcelVo>();
        if (query.getTotalCount() > 0) {
            AccountQuotaDailyQuery sumQuery = new AccountQuotaDailyQuery();
            BeanUtils.copyProperties(query, sumQuery);
            AccountQuotaDaily sumResult = accountQuotaDailyService.sumByQuery(sumQuery);
            
            for(AccountQuotaDaily temp : query.getData()){
                AccountQuotaDailyExcelVo data = new AccountQuotaDailyExcelVo();
                data.setReportDate(DateUtil.getDateFormat(temp.getReportDate(), DateUtil.DATE_PATTERN));
                data.setQuotaName(temp.getQuotaName());
                data.setDealTransIn(temp.getDealTransIn());
                data.setDealTransOut(temp.getDealTransOut());
                data.setExchangeTransOut(temp.getExchangeTransOut());
                exportData.add(data);
            }
            
            if (sumResult != null) {
                AccountQuotaDailyExcelVo data = new AccountQuotaDailyExcelVo();
                data.setReportDate("汇总");
                data.setDealTransIn(sumResult.getDealTransIn());
                data.setDealTransOut(sumResult.getDealTransOut());
                data.setExchangeTransOut(sumResult.getExchangeTransOut());
                exportData.add(data);
            }
        }
        EasyExcelUtil.writeExcel(response, exportData, "配额每日统计表", "配额每日统计表", AccountQuotaDailyExcelVo.class);
    }
    
    private void checkRequest(AccountQuotaDailyQuery query){
        if (query.getStartDate() == null) {
            query.setStartDate(DateUtils.truncate(new Date(),Calendar.MONTH));
        }
        if (query.getEndDate() == null) {
            query.setEndDate(Calendar.getInstance().getTime());
        }
        DateUtils.setHours(query.getEndDate(), 23);
        DateUtils.setMinutes(query.getEndDate(), 59);
        DateUtils.setSeconds(query.getEndDate(), 59);
    }
}
